


#include <iostream>
#include <cassert>
using namespace std;



char* sortSentence(char* s)
{
    int sz = strlen(s);
    int index = 0;
    char* ans = (char*)malloc(sizeof(char) * 202);
    assert(ans);

    char cnt = '1';
    while (1)
    {
        int beg = 0, end = 0;
        while (end < sz && s[end] != cnt)
        {
            ++end;

            if (s[end] == ' ')
                beg = end + 1;
        }

        if (end < sz)
        {
            for (int i = beg; i < end; ++i)
            {
                ans[index++] = s[i];
            }
            ans[index++] = ' ';
        }
        else
        {
            break;
        }

        ++cnt;
    }

    ans[--index] = '\0';

    return ans;
}

int findLHS(int* nums, int sz)
{
    int ans = 0;

    for (int i = 0; i < sz; ++i)
    {
        int cnt = 0;
        for (int j = i + 1; j < sz; ++j)
        {
            if (1 == abs(nums[i] - nums[j]))
                ++cnt;
        }
        ans = (int)fmax(ans, cnt);
    }

    return ans == 0 ? 0 : ++ans;
}

int compare(const void* p1, const void* p2)
{
    return *((int*)p1) - *((int*)p2);
}

// err
int findLHS(int* nums, int sz)
{
    if (1 == sz)
        return 0;

    qsort(nums, sz, sizeof(nums[0]), compare);

    int ans = 0;
    int left = 0, right = 1;
    while (right < sz)
    {
        while (right < sz && nums[right] - nums[left] <= 1)
        {
            ++right;
        }
        ans = fmax(ans, right - left);

        while (left < right && nums[right] - nums[left] > 1)
        {
            ++left;
        }
    }

    return ans;
}

void Test1()
{
    int nums[] = { 1,3,2,2,5,2,3,7 };
    int sz = sizeof(nums) / sizeof(nums[0]);

    cout << findLHS(nums, sz) << endl;
}

int main()
{
    Test1();

	return 0;
}